package com.im.tcp;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeoutException;

@Slf4j
public class MQFactory {

    private static Map<String, Channel> mqChannelMap=null;
    private static  ConnectionFactory connectionFactory=null;

    public static Channel getChannel(String channelName) throws IOException, TimeoutException {
        Channel channel= mqChannelMap.get(channelName);
        if(channel==null){
               channel=connectionFactory.newConnection().createChannel();
               mqChannelMap.put(channelName,channel);
        }
        return channel;
    }



    public static void init(ConnectionFactory conFactory){
        connectionFactory=conFactory;
        mqChannelMap=new ConcurrentHashMap<>();
        log.info("MQ工厂初始化完成！");
    }


}
